package com.lolxxs.service.impl;

import com.lolxxs.annotation.MyCacheEvict;
import com.lolxxs.annotation.MyCachePut;
import com.lolxxs.annotation.MyCacheable;
import com.lolxxs.model.User;
import com.lolxxs.mapper.UserMapper;
import com.lolxxs.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenyanting
 * @since 2022-04-03
 */
@Service("userServiceImpl")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

//    @Override
//    @Cacheable(value = "userCache", key = "#id", unless = "#result == null")
    @MyCacheable(value = "userCache", key = "id")
    public User getUserById(Long id) {
        System.out.println("运行");
        return userMapper.selectById(id);
    }

//    @Override
//    @CachePut(value = "userCache", key = "#user.id", unless = "#result == null")
    @MyCachePut(value = "userCache", key = "id")
    public User updateUser(Long id, User user) {
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());
    }

//    @Override
//    @CacheEvict(value = "userCache", key = "#id")
    @MyCacheEvict(value = "userCache", key = "id")
    public void deleteUserById(Long id) {
        userMapper.deleteById(id);
    }
}
